<?php
declare(strict_types=1);

namespace App\Middleware;

use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Utils\Context;
use Hyperf\Utils\Contracts\Arrayable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;


class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{


    public function dispatch(ServerRequestInterface $request): ServerRequestInterface
    {

        $path = $request->getUri()->getPath();
        $path_arr = explode('_',$path);  //分割路径

        $path_res = '';
        foreach ($path_arr as $key => $path_str)
            if( $key)   //如果不是第一个字符串
                $path_res .= ucfirst($path_str);
            else
                $path_res .= $path_str;

        $routes = $this->dispatcher->dispatch($request->getMethod(), $path_res);
        $dispatched = new Dispatched($routes);
        return Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, $dispatched));
    }

}